package com.fortune.controller;

import java.math.BigDecimal;
import java.util.List;
import java.util.UUID;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;

import com.fortune.base.BaseController;
import com.fortune.dto.excution.ReturnDepositExcution;
import com.fortune.dto.in.returnDeposit.AddReturnDepositDto;
import com.fortune.dto.in.returnDeposit.ReturnDepositEditDto;
import com.fortune.enums.ReturnDepositEnum;
import com.fortune.exception.db.InsertInnerErrorException;
import com.fortune.service.ReturnDepositService;
import com.fortune.util.UUIDUtil;
import com.fortune.vo.returnDeposit.*;
import org.hibernate.validator.constraints.NotBlank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.fortune.base.BaseAPIExcution;
import com.fortune.dto.in.returnDeposit.AddDto;
import com.fortune.dto.in.returnDeposit.EditDto;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@Api(value = "wechat/returnDeposit", description = "工单相关", tags = {"微信小程序（业务员）：工单管理"})
@RestController
@RequestMapping("wechat/returnDeposit")
public class ReturnDepositController extends BaseController{
	@Autowired
	private ReturnDepositService returnDepositService;

	@ApiOperation(value = "工单列表", nickname = "returnDepositList", notes = "", tags={ "@郭阳" })
	@ApiImplicitParams({
		@ApiImplicitParam(name="type", value="类型（1未处理2已批准3已接货4已完成）",paramType="query",dataType="int",allowableValues="1,2,3,4"),
		@ApiImplicitParam(name="Authorization", value="登录返回的token值",paramType="header",dataType="String",required = true)
	})
    @GetMapping(value = "list",
        produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<List<ReturnDepositInfo>> list(@Validated @Min(value=1) @Max(value=4) int type) {
		String did = getDId();
		String userId = getUserId();
		if(type == 1){
			List<ReturnDepositInfo> findReturnDepositInfoByUn = returnDepositService.findReturnDepositInfoByUn(did, userId);
			return new ReturnDepositExcution<List<ReturnDepositInfo>>(findReturnDepositInfoByUn, ReturnDepositEnum.FIND_SUCCESS);
		}
		if(type == 2){
			List<ReturnDepositInfo> findReturnDepositInfoByApproved = returnDepositService.findReturnDepositInfoByApproved(did,userId);
			return new ReturnDepositExcution<List<ReturnDepositInfo>>(findReturnDepositInfoByApproved, ReturnDepositEnum.FIND_SUCCESS);
		}
		if(type == 3){
			List<ReturnDepositInfo> findReturnDepositInfoByReceived = returnDepositService.findReturnDepositInfoByReceived(did,userId);
			return new ReturnDepositExcution<List<ReturnDepositInfo>>(findReturnDepositInfoByReceived, ReturnDepositEnum.FIND_SUCCESS);
		}
		if(type == 4){
			List<ReturnDepositInfo> findReturnDepositInfoByCompleted = returnDepositService.findReturnDepositInfoByCompleted(did, userId);
			return new ReturnDepositExcution<List<ReturnDepositInfo>>(findReturnDepositInfoByCompleted, ReturnDepositEnum.FIND_SUCCESS);
		}
		return new ReturnDepositExcution<List<ReturnDepositInfo>>(null, ReturnDepositEnum.FIND_ERROR);
	}

	@ApiOperation(value = "添加工单", nickname = "returnDepositAdd", notes = "", tags={ "@郭阳" })
	@ApiImplicitParams({
			@ApiImplicitParam(name="Authorization", value="登录返回的token值",paramType="header",dataType="String",required = true)
	})
    @PostMapping(value = "add",
        produces = { MediaType.APPLICATION_JSON_VALUE },
        consumes = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> add(@Validated @RequestBody AddDto addDto) {
		String did = getDId();
		String userId = getUserId();
		String crdid = UUIDUtil.get32UUID();//工单id
		String clientId = addDto.getClientId();//客户id
		String orderNo = addDto.getOrderNo();//工单编号
		int addReturnDeposit = returnDepositService.addReturnDeposit(did, clientId, crdid, orderNo, userId);
		if (addReturnDeposit < 1) {
			return new ReturnDepositExcution<Void>(null, ReturnDepositEnum.ADD_ERROR);
		}
		List<AddReturnDepositDto> addReturnDepositDto = addDto.getAddReturnDepositDto();
		for(int i = 0; i < addReturnDepositDto.size(); i++){
			AddReturnDepositDto addReturnDepositDto1 = addReturnDepositDto.get(i);
			String productId = addReturnDepositDto1.getProductId();
			float productCount = addReturnDepositDto1.getProductCount();
			BigDecimal depositPrice = addReturnDepositDto1.getDepositPrice();
			int orderNum = addReturnDepositDto1.getOrderNum();
			String mid = addReturnDepositDto1.getMid();
			int addReturnDepositDetails = returnDepositService.addReturnDepositDetails(did, clientId, crdid, orderNum, mid, productId, productCount, depositPrice);
			if (addReturnDepositDetails < 1) {
				throw new InsertInnerErrorException("添加失败");
			}
		}
		return new ReturnDepositExcution<Void>(null, ReturnDepositEnum.ADD_SUCCESS);
	}
	
	@ApiOperation(value = "编辑工单", nickname = "returnDepositEdit", notes = "", tags={ "@郭阳" })
	@ApiImplicitParams({
			@ApiImplicitParam(name="Authorization", value="登录返回的token值",paramType="header",dataType="String",required = true)
	})
    @PutMapping(value = "edit",
        produces = { MediaType.APPLICATION_JSON_VALUE },
        consumes = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> edit(@Validated @RequestBody ReturnDepositEditDto returnDepositEditDto) {
		String did = getDId();
		int delReturnDeposit = returnDepositService.delReturnDeposit(did, returnDepositEditDto);
		if(delReturnDeposit < 1){
			return new ReturnDepositExcution<Void>(null, ReturnDepositEnum.EDIT_ERROR);
		}
		String returnDepositId = returnDepositEditDto.getReturnDepositId();
		String clientId = returnDepositEditDto.getClientId();
		List<EditDto> editDtos = returnDepositEditDto.getEditDtos();
		for(int i=0; i<editDtos.size(); i++){
			EditDto editDto = editDtos.get(i);
			int orderNum = editDto.getOrderNum();
			String mid = editDto.getMid();
			String productId = editDto.getProductId();
			int productCount = editDto.getProductCount();
			BigDecimal depositPrice = editDto.getDepositPrice();
			int addReturnDepositDetails = returnDepositService.addReturnDepositDetails(did, clientId, returnDepositId, orderNum, mid, productId, productCount, depositPrice);
			if (addReturnDepositDetails < 1) {
				return new ReturnDepositExcution<Void>(null, ReturnDepositEnum.EDIT_ERROR);
			}
		}
		return new ReturnDepositExcution<Void>(null, ReturnDepositEnum.EDIT_SUCCESS);
	}
	
	@ApiOperation(value = "删除工单", nickname = "returnDepositDelete", notes = "", tags={ "@郭阳" })
	@ApiImplicitParams({
			@ApiImplicitParam(name="Authorization", value="登录返回的token值",paramType="header",dataType="String",required = true)
	})
	@ApiImplicitParam(name="id", value="工单id",paramType="query",dataType="string")
    @DeleteMapping(value = "delete",
        produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> delete(@Validated @NotBlank(message="工单id不能为空") String id) {
		String did = getDId();
		int deleteReturnDeposit = returnDepositService.deleteReturnDeposit(did, id);
		if(deleteReturnDeposit <1){
			return new ReturnDepositExcution<Void>(null, ReturnDepositEnum.DELETE_ERROR);
		}
		return new ReturnDepositExcution<Void>(null, ReturnDepositEnum.DELETE_SUCCESS);
	}

	@ApiOperation(value = "退押金工单详情", nickname = "depositDetails", notes = "", tags={ "@郭阳" })
	@ApiImplicitParams({
			@ApiImplicitParam(name="Authorization", value="登录返回的token值",paramType="header",dataType="String",required = true),
			@ApiImplicitParam(name="wOrderId", value="工单ID",paramType="query",dataType="String"),
			@ApiImplicitParam(name="clientId", value="客户id",paramType="query",dataType="String"),
			@ApiImplicitParam(name="type", value="类型（1已提交2已批准3接货完成4已入库）",paramType="query",dataType="int",allowableValues="1,2,3,4")
	})
	@GetMapping(value = "depositDetails",
			produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<?> depositDetails(@Validated @NotBlank(message="工单id不能为空") String wOrderId,
											@Validated @NotBlank(message="客户id不能为空") String clientId,
									    	@Validated @Min(value=1) @Max(value=4) int type) {
		String did = getDId();
		/*查询客户信息*/
		ClientNamePhone findClientNamePhone = returnDepositService.findClientNamePhone(clientId);
		String clientName = findClientNamePhone.getClientName();
		String clientPhone = findClientNamePhone.getClientPhone();
		String address = findClientNamePhone.getAddress();
		float longitude = findClientNamePhone.getLongitude();
		float latitude = findClientNamePhone.getLatitude();
		if(type == 1){
			DepositDetailsSubmit findDepositDetailsSubmit = returnDepositService.findDepositDetailsSubmit(did, wOrderId, clientId);
			if(findDepositDetailsSubmit == null){
				return new ReturnDepositExcution<DepositDetailsSubmit>(null, ReturnDepositEnum.FIND_ERROR);
			}
			findDepositDetailsSubmit.setClientName(clientName);
			findDepositDetailsSubmit.setClientPhone(clientPhone);
			findDepositDetailsSubmit.setAddress(address);
			findDepositDetailsSubmit.setLongitude(longitude);
			findDepositDetailsSubmit.setLongitude(latitude);
			String createTime = findDepositDetailsSubmit.getCreateTime();
			findDepositDetailsSubmit.setCreateTime(createTime.substring(0,19));
			return new ReturnDepositExcution<DepositDetailsSubmit>(findDepositDetailsSubmit, ReturnDepositEnum.FIND_SUCCESS);
		}
		if(type == 2){
			DepositDetailsApproved findDepositDetailsApproveds = returnDepositService.findDepositDetailsApproveds(did, wOrderId, clientId);
			if(findDepositDetailsApproveds == null){
				return new ReturnDepositExcution<DepositDetailsApproved>(null, ReturnDepositEnum.FIND_ERROR);
			}
			findDepositDetailsApproveds.setClientName(clientName);
			findDepositDetailsApproveds.setClientPhone(clientPhone);
			findDepositDetailsApproveds.setAddress(address);
			findDepositDetailsApproveds.setLongitude(longitude);
			findDepositDetailsApproveds.setLongitude(latitude);
			String createTime = findDepositDetailsApproveds.getCreateTime();
			findDepositDetailsApproveds.setCreateTime(createTime.substring(0,19));
			return new ReturnDepositExcution<DepositDetailsApproved>(findDepositDetailsApproveds, ReturnDepositEnum.FIND_SUCCESS);
		}
		if(type == 3){
			DepositDetailsApproved findDepositDetailsStorage = returnDepositService.findDepositDetailsStorage(did, wOrderId, clientId);
			if(findDepositDetailsStorage == null){
				return new ReturnDepositExcution<DepositDetailsApproved>(null, ReturnDepositEnum.FIND_ERROR);
			}
			findDepositDetailsStorage.setClientName(clientName);
			findDepositDetailsStorage.setClientPhone(clientPhone);
			findDepositDetailsStorage.setAddress(address);
			findDepositDetailsStorage.setLongitude(longitude);
			findDepositDetailsStorage.setLongitude(latitude);
			String createTime = findDepositDetailsStorage.getCreateTime();
			findDepositDetailsStorage.setCreateTime(createTime.substring(0,19));
			return new ReturnDepositExcution<DepositDetailsApproved>(findDepositDetailsStorage, ReturnDepositEnum.FIND_SUCCESS);
		}
		if(type == 4){
			DepositDetailsApproved findDepositDetailsCarry = returnDepositService.findDepositDetailsCarry(did, wOrderId, clientId);
			if(findDepositDetailsCarry == null){
				return new ReturnDepositExcution<DepositDetailsApproved>(null, ReturnDepositEnum.FIND_ERROR);
			}
			findDepositDetailsCarry.setClientName(clientName);
			findDepositDetailsCarry.setClientPhone(clientPhone);
			findDepositDetailsCarry.setAddress(address);
			findDepositDetailsCarry.setLongitude(longitude);
			findDepositDetailsCarry.setLongitude(latitude);
			String createTime = findDepositDetailsCarry.getCreateTime();
			findDepositDetailsCarry.setCreateTime(createTime.substring(0,19));
			return new ReturnDepositExcution<DepositDetailsApproved>(findDepositDetailsCarry, ReturnDepositEnum.FIND_SUCCESS);
		}
		return new ReturnDepositExcution<DepositDetailsApproved>(null, ReturnDepositEnum.FIND_ERROR);
	}

	/*@ApiOperation(value = "退押金工单已批准详情", nickname = "depositDetailsApproved", notes = "", tags={ "@郭阳" })
	@ApiImplicitParams({
			@ApiImplicitParam(name="Authorization", value="登录返回的token值",paramType="header",dataType="String",required = true),
			@ApiImplicitParam(name="wOrderId", value="工单ID",paramType="query",dataType="String"),
			@ApiImplicitParam(name="clientId", value="客户id",paramType="query",dataType="String")
	})
	@GetMapping(value = "depositDetailsApproved",
			produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<DepositDetailsApproved> depositDetailsApproved(@Validated @NotBlank(message="工单id不能为空") String wOrderId,
																		   @Validated @NotBlank(message="客户id不能为空") String clientId) {
		String did = getDId();
		DepositDetailsApproved findDepositDetailsApproveds = returnDepositService.findDepositDetailsApproveds(did, wOrderId, clientId);
		if(findDepositDetailsApproveds == null){
			return new ReturnDepositExcution<DepositDetailsApproved>(null, ReturnDepositEnum.FIND_ERROR);
		}
		return new ReturnDepositExcution<DepositDetailsApproved>(findDepositDetailsApproveds, ReturnDepositEnum.FIND_SUCCESS);
	}*/

	/*@ApiOperation(value = "退押金工单接货完成详情", nickname = "depositDetailsReceive", notes = "", tags={ "@郭阳" })
	@ApiImplicitParams({
			@ApiImplicitParam(name="Authorization", value="登录返回的token值",paramType="header",dataType="String",required = true),
			@ApiImplicitParam(name="wOrderId", value="工单ID",paramType="query",dataType="String"),
			@ApiImplicitParam(name="clientId", value="客户id",paramType="query",dataType="String")
	})
	@GetMapping(value = "depositDetailsReceive",
			produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<DepositDetailsApproved> depositDetailsReceive(@Validated @NotBlank(message="工单id不能为空") String wOrderId,
																		  @Validated @NotBlank(message="客户id不能为空") String clientId) {
		String did = getDId();
		DepositDetailsApproved findDepositDetailsStorage = returnDepositService.findDepositDetailsStorage(did, wOrderId, clientId);
		if(findDepositDetailsStorage == null){
			return new ReturnDepositExcution<DepositDetailsApproved>(null, ReturnDepositEnum.FIND_ERROR);
		}
		return new ReturnDepositExcution<DepositDetailsApproved>(findDepositDetailsStorage, ReturnDepositEnum.FIND_SUCCESS);
	}

	@ApiOperation(value = "退押金工单已入库详情", nickname = "depositDetailsStorage", notes = "", tags={ "@郭阳" })
	@ApiImplicitParams({
			@ApiImplicitParam(name="Authorization", value="登录返回的token值",paramType="header",dataType="String",required = true),
			@ApiImplicitParam(name="wOrderId", value="工单ID",paramType="query",dataType="String"),
			@ApiImplicitParam(name="clientId", value="客户id",paramType="query",dataType="String")
	})
	@GetMapping(value = "depositDetailsStorage",
			produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<DepositDetailsApproved> depositDetailsStorage(@Validated @NotBlank(message="工单id不能为空") String wOrderId,
																		 @Validated @NotBlank(message="客户id不能为空") String clientId) {
		String did = getDId();
		DepositDetailsApproved findDepositDetailsCarry = returnDepositService.findDepositDetailsCarry(did, wOrderId, clientId);
		if(findDepositDetailsCarry == null){
			return new ReturnDepositExcution<DepositDetailsApproved>(null, ReturnDepositEnum.FIND_ERROR);
		}
		return new ReturnDepositExcution<DepositDetailsApproved>(findDepositDetailsCarry, ReturnDepositEnum.FIND_SUCCESS);
	}*/

	@ApiOperation(value = "申请退押金工单商品列表", nickname = "depositProductDetails", notes = "", tags={ "@郭阳" })
	@ApiImplicitParams({
			@ApiImplicitParam(name="Authorization", value="登录返回的token值",paramType="header",dataType="String",required = true),
			@ApiImplicitParam(name="clientId", value="客户id",paramType="query",dataType="String")
	})
	@GetMapping(value = "productDetails",
			produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<List<DepositProductDetails>> productDetails(@Validated @NotBlank(message="客户id不能为空") String clientId) {
		String did = getDId();
		List<DepositProductDetails> findDepositProductDetails = returnDepositService.findDepositProductDetails(did, clientId);
		if(findDepositProductDetails == null){
			return new ReturnDepositExcution<List<DepositProductDetails>>(null, ReturnDepositEnum.FIND_ERROR);
		}
		return new ReturnDepositExcution<List<DepositProductDetails>>(findDepositProductDetails, ReturnDepositEnum.FIND_SUCCESS);
	}

	@ApiOperation(value = "申请退押金工单商品列表(后台管理)", nickname = "depositProductDetailsByHt", notes = "", tags={ "@郭阳" })
	@ApiImplicitParams({
			@ApiImplicitParam(name="Authorization", value="登录返回的token值",paramType="header",dataType="String",required = true),
	})
	@GetMapping(value = "productDetailsByHt",
			produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<List<DepositProductDetails>> productDetailsByHt() {
		String did = getDId();
		List<DepositProductDetails> findDepositProductDetailsByHt = returnDepositService.findDepositProductDetailsByHt(did);
		if(findDepositProductDetailsByHt == null){
			return new ReturnDepositExcution<List<DepositProductDetails>>(null, ReturnDepositEnum.FIND_ERROR);
		}
		return new ReturnDepositExcution<List<DepositProductDetails>>(findDepositProductDetailsByHt, ReturnDepositEnum.FIND_SUCCESS);
	}
}
